package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obCongNo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CongNo extends Activity {
	LinearLayout llMainCongNo;
	// private String strNhomChon = "";
	private String strKhoChon = "";
	ArrayList<Integer> date_actual;
	private String date;
	private TableRow rowFocus;
	private Integer choose_cn = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bc_congno);

		llMainCongNo = (LinearLayout) findViewById(R.id.llMailnBCCongNo);
		strKhoChon = super.getIntent().getExtras().getString("strKhoChon");
		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_tk_cn");
		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));
		choose_cn = super.getIntent().getExtras().getInt("chose_cn");

		loadData();
	}

	public void loadData() {
		String chiNhString = getChiNhanhByKho(strKhoChon);
		Float totalNo = 0.0f, totalNoByNhomKH = 0.0f;
		// ten Khach hang
		String tenNhomKH = "";
		Boolean isFirst = true;
		;

		String sql = " spmobileCongNoKH_TheoChiNhanh '" + chiNhString + "','"
				+ date + "','" + date + "'";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		final TableLayout table = new TableLayout(this);
		llMainCongNo.addView(table);
		TableRow trHead = new TableRow(this);
		trHead.setWeightSum(5.0f);

		TextView tvMaKH = new TextView(this);
		tvMaKH.setText("Mã KH");
		TK_TongMatHang.setLayoutForTextView(tvMaKH, true, Gravity.CENTER, 10,
				2.0f);

		TextView tvTenKH = new TextView(this);
		tvTenKH.setText("Tên KH");
		TK_TongMatHang.setLayoutForTextView(tvTenKH, true, Gravity.CENTER, 10,
				1.5f);

		TextView tvNoCuoiKy = new TextView(this);
		tvNoCuoiKy.setText("Nợ Cuối Kỳ");
		TK_TongMatHang.setLayoutForTextView(tvNoCuoiKy, true, Gravity.CENTER,
				10, 1.5f, true);

		trHead.addView(tvMaKH);
		trHead.addView(tvTenKH);
		trHead.addView(tvNoCuoiKy);
		table.addView(trHead);
		try {
			while (rs.next()) {
				final obCongNo congNo = new obCongNo();
				if (rs.getString("NoCuoiKy") != null) {
					congNo.setNoCuoiKy(rs.getString("NoCuoiKy"));
				}
				int checkCN = checkCongNo(congNo.getNoCuoiKy());
				if (checkCN == 0 || checkCN == 1) {
					if (rs.getString("Supplier ID") != null) {
						congNo.setMaKH(rs.getString("Supplier ID"));
					}
					if (rs.getString("Company Name") != null) {
						congNo.setTenKH(rs.getString("Company Name"));
					}
					if (rs.getString("NoCuoiKy") != null) {
						congNo.setNoCuoiKy(rs.getString("NoCuoiKy"));
					}
					if (isFirst) {
						if (rs.getString("NhomKH") != null) {
							tenNhomKH = rs.getString("NhomKH");
							isFirst = false;
						}
					} else {
						if (rs.getString("NhomKH") != null) {
							if (!tenNhomKH.equals(rs.getString("NhomKH"))) {
								// add date in textview
								final TableRow trHeadNhoms = new TableRow(this);
								trHeadNhoms
										.setOnClickListener(new OnClickListener() {
											public void onClick(View v) {
												try {
													if (rowFocus != null)
														rowFocus.setBackgroundColor(Color.TRANSPARENT);
													rowFocus = (TableRow) v;
													v.setBackgroundColor(Color.YELLOW);
												} catch (Exception e) {
												}
											}
										});
								trHeadNhoms.setWeightSum(5.0f);

								TextView tvTenNhomKHs = new TextView(this);
								tvTenNhomKHs.setText(tenNhomKH);
								TK_TongMatHang.setLayoutForTextView(
										tvTenNhomKHs, false, Gravity.CENTER,
										10,3.54f);

								TextView tvTotalNoCuoiKys = new TextView(this);
								tvTotalNoCuoiKys.setText(Global
										.formatDecimal(totalNoByNhomKH));
								TK_TongMatHang.setLayoutForTextView(
										tvTotalNoCuoiKys, false, Gravity.RIGHT,
										10, 1.46f, true);

								trHeadNhoms.addView(tvTenNhomKHs);
								trHeadNhoms.addView(tvTotalNoCuoiKys);
								table.addView(trHeadNhoms);
								tenNhomKH = rs.getString("NhomKH");
								totalNoByNhomKH =0.0f;
							}
						}
						isFirst = false;
					}

					// int checkCN = checkCongNo(congNo.getNoCuoiKy());
					// cong no phai thu
					if (checkCN == 0
							&& choose_cn.equals(TK_ChonMau.CHOOSE_CNTHU)) {
						// add date in textview
						final TableRow trHeads = new TableRow(this);
						trHeads.setOnClickListener(new OnClickListener() {
							public void onClick(View v) {
								try {
									if (rowFocus != null)
										rowFocus.setBackgroundColor(Color.TRANSPARENT);
									rowFocus = (TableRow) v;
									v.setBackgroundColor(Color.YELLOW);
								} catch (Exception e) {
								}
							}
						});
						trHeads.setWeightSum(5.0f);

						TextView tvMaKHs = new TextView(this);
						tvMaKHs.setText(congNo.getMaKH());
						TK_TongMatHang.setLayoutForTextView(tvMaKHs, false,
								Gravity.LEFT, 10, 2.0f);

						TextView tvTenKHs = new TextView(this);
						tvTenKHs.setText(congNo.getTenKH());
						TK_TongMatHang.setLayoutForTextView(tvTenKHs, false,
								Gravity.CENTER, 10, 1.5f);

						TextView tvNoCuoiKys = new TextView(this);
						tvNoCuoiKys.setText(Global.formatDecimal(Float
								.valueOf(congNo.getNoCuoiKy())));
						TK_TongMatHang.setLayoutForTextView(tvNoCuoiKys, false,
								Gravity.RIGHT, 10, 1.5f, true);

						trHeads.addView(tvMaKHs);
						trHeads.addView(tvTenKHs);
						trHeads.addView(tvNoCuoiKys);
						table.addView(trHeads);

						totalNo = totalNo + Float.valueOf(congNo.getNoCuoiKy());
						totalNoByNhomKH = totalNoByNhomKH + Float.valueOf(congNo.getNoCuoiKy());
					} else if (checkCN == 1
							&& choose_cn.equals(TK_ChonMau.CHOOSE_CNTRA)) {// cong
																			// no
																			// phai
																			// tr
						// add date in textview
						final TableRow trHeads = new TableRow(this);
						trHeads.setOnClickListener(new OnClickListener() {
							public void onClick(View v) {
								try {
									if (rowFocus != null)
										rowFocus.setBackgroundColor(Color.TRANSPARENT);
									rowFocus = (TableRow) v;
									v.setBackgroundColor(Color.YELLOW);
								} catch (Exception e) {
								}
							}
						});
						trHeads.setWeightSum(5.0f);

						TextView tvMaKHs = new TextView(this);
						tvMaKHs.setText(congNo.getMaKH());
						TK_TongMatHang.setLayoutForTextView(tvMaKHs, false,
								Gravity.LEFT, 10, 2.0f);

						TextView tvTenKHs = new TextView(this);
						tvTenKHs.setText(congNo.getTenKH());
						TK_TongMatHang.setLayoutForTextView(tvTenKHs, false,
								Gravity.CENTER, 10, 1.5f);

						TextView tvNoCuoiKys = new TextView(this);
						tvNoCuoiKys.setText(Global.formatDecimal(Float
								.valueOf(congNo.getNoCuoiKy())));
						TK_TongMatHang.setLayoutForTextView(tvNoCuoiKys, false,
								Gravity.RIGHT, 10, 1.5f, true);

						trHeads.addView(tvMaKHs);
						trHeads.addView(tvTenKHs);
						trHeads.addView(tvNoCuoiKys);
						table.addView(trHeads);

						totalNo = totalNo + Float.valueOf(congNo.getNoCuoiKy());
						totalNoByNhomKH = totalNoByNhomKH + Float.valueOf(congNo.getNoCuoiKy());
					}
					
					if(rs.isLast())
					{
						// add date in textview
						final TableRow trHeadNhoms = new TableRow(this);
						trHeadNhoms
								.setOnClickListener(new OnClickListener() {
									public void onClick(View v) {
										try {
											if (rowFocus != null)
												rowFocus.setBackgroundColor(Color.TRANSPARENT);
											rowFocus = (TableRow) v;
											v.setBackgroundColor(Color.YELLOW);
										} catch (Exception e) {
										}
									}
								});
						trHeadNhoms.setWeightSum(5.0f);

						TextView tvTenNhomKHs = new TextView(this);
						tvTenNhomKHs.setText(tenNhomKH);
						TK_TongMatHang.setLayoutForTextView(
								tvTenNhomKHs, false, Gravity.CENTER,
								10, 3.54f);

						TextView tvTotalNoCuoiKys = new TextView(this);
						tvTotalNoCuoiKys.setText(Global
								.formatDecimal(totalNoByNhomKH));
						TK_TongMatHang.setLayoutForTextView(
								tvTotalNoCuoiKys, false, Gravity.RIGHT,
								10, 1.46f, true);

						trHeadNhoms.addView(tvTenNhomKHs);
						trHeadNhoms.addView(tvTotalNoCuoiKys);
						table.addView(trHeadNhoms);
					}
				} else
					continue;
			}
			// add date in textview
			TableRow trHeadEnd = new TableRow(this);
			trHeadEnd.setWeightSum(5.0f);

//			TextView tvMaKHEnd = new TextView(this);
//			tvMaKHEnd.setText("");
//			TK_TongMatHang.setLayoutForTextView(tvMaKHEnd, false,
//					Gravity.CENTER, 10, 2.0f);
//			tvMaKHEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvTenKHEnd = new TextView(this);
			tvTenKHEnd.setText("Tổng");
			TK_TongMatHang.setLayoutForTextView(tvTenKHEnd, false,
					Gravity.CENTER, 10, 3.54f);
			tvTenKHEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvNoCuoiKyEnd = new TextView(this);
			tvNoCuoiKyEnd.setText(Global.formatDecimal(totalNo).toString());
			TK_TongMatHang.setLayoutForTextView(tvNoCuoiKyEnd, false,
					Gravity.RIGHT, 10, 1.46f, true);
			tvNoCuoiKyEnd.setBackgroundResource(R.layout.border);
//			trHeadEnd.addView(tvMaKHEnd);
			trHeadEnd.addView(tvTenKHEnd);
			trHeadEnd.addView(tvNoCuoiKyEnd);
			table.addView(trHeadEnd);
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public String getChiNhanhByKho(String maKho) {
		String chiNhanh = "";
		String sql = " SELECT ChiNhanhID FROM kho WHERE MSK = '" + strKhoChon
				+ "'";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			while (rs.next()) {
				chiNhanh = rs.getString(1);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return chiNhanh;
	}

	/**
	 * @author Administrator
	 * @param noCuoiKy
	 * @return check Cong no phai thu,Cong no phai tra
	 */
	public int checkCongNo(String noCuoiKy) {
		Integer value = Math.round(Float.valueOf(noCuoiKy));
		// cong no phai thu
		if (value > 0)
			return 0;
		// cong no phai tra
		else if (value < 0)
			return 1;
		else
			return 3;
	}
}
